﻿using System.Security.Cryptography;
using System.Text;

namespace MyWeb.ServiceBus.Common
{
    public class Encrypt
    {
        /// <summary>
        /// BASE64编码加密
        /// </summary>
        /// <param name="s"></param>
        /// <param name="encode"></param>
        /// <returns></returns>
        public static string Base64Encode(string s, string encode = "utf-8")
        {
            return Convert.ToBase64String(Encoding.GetEncoding(encode).GetBytes(s));
        }

        /// <summary>
        /// BASE64编码解密
        /// </summary>
        /// <param name="s"></param>
        /// <param name="encode"></param>
        /// <returns></returns>
        public static string Base64Decode(string s, string encode = "utf-8")
        {
            return Encoding.GetEncoding(encode).GetString(Convert.FromBase64String(s));
        }


        public static string MD5Encrypt(string source, int length = 32)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] targetData = md5.ComputeHash(Encoding.UTF8.GetBytes(source));

            string byte2String = null;

            for (int i = 0; i < targetData.Length; i++)
            {
                byte2String += targetData[i].ToString("x2");
            }

            return byte2String.ToUpper();
        }
    }
}